package com.yxzx.ebs.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxzx.common.YxzxResult;
import com.yxzx.ebs.entity.EbsCourse;
import com.yxzx.ebs.entity.EbsCourseDescription;
import com.yxzx.ebs.entity.vo.CourseInfoForm;
import com.yxzx.ebs.entity.vo.CourseQueryVo;
import com.yxzx.ebs.service.EbsCourseDescriptionService;
import com.yxzx.ebs.service.EbsCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author kongfuzi
 * @since 2020-07-30
 */
@RestController
@CrossOrigin
@Api(tags = "课程管理")
@Slf4j
@RequestMapping("/eduservice/course")
public class EbsCourseController {

    @Autowired
    private EbsCourseService courseService;
    @Autowired
    private EbsCourseDescriptionService courseDescriptionService;

    @ApiOperation("添加课程信息")
    @PostMapping
    public YxzxResult addCourseInfo(@ApiParam(name = "courseInfoForm",value = "封装课程信息对象",required = true)
                                    @RequestBody CourseInfoForm courseInfoForm){
       String courseId =  courseService.addCourseInfo(courseInfoForm);
       return courseId != null ? YxzxResult.ok().data("courseId",courseId) : YxzxResult.error().message("添加课程信息失败");
    }

    @ApiOperation("根据ID查询课程信息")
    @GetMapping("{id}")
    public YxzxResult getById(@ApiParam(name = "ID",value = "课程ID",required = true) @PathVariable String id){
        CourseInfoForm courseInfoForm = courseService.getCourseInfoById(id);
        return courseInfoForm != null ? YxzxResult.ok().data("item",courseInfoForm) : YxzxResult.error();
    }

    @ApiOperation("更新课程信息")
    @PostMapping("updateCourse")
    public YxzxResult updateCourseInfo(@ApiParam(name = "courseInfoForm",value = "封装课程信息对象",required = true)
                                           @RequestBody CourseInfoForm courseInfoForm){
        EbsCourse ebsCourse = new EbsCourse();
        BeanUtils.copyProperties(courseInfoForm,ebsCourse);
        QueryWrapper<EbsCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("id",courseInfoForm.getId());
        //1.修改课程基本信息表
        boolean b = courseService.update(ebsCourse, wrapper);
        //2.修改课程描述信息表
        EbsCourseDescription description = new EbsCourseDescription();
        description.setDescription(courseInfoForm.getDescription());
        description.setId(courseInfoForm.getId());
        boolean b1 = courseDescriptionService.updateById(description);
        return b&&b1 ? YxzxResult.ok().message("更新课程基本信息成功") : YxzxResult.error();
    }

    @ApiOperation("分页多条件查询课程列表")
    @PostMapping("{page}/{limit}")
    public YxzxResult pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody CourseQueryVo courseQueryVo){
        Page<EbsCourse> pageParam = new Page<>(page,limit);
        courseService.getCourseInfoByPage(pageParam,courseQueryVo);
        List<EbsCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return YxzxResult.ok().data("total",total).data("items",records);
    }

    @ApiOperation("根据ID删除课程信息")
    @DeleteMapping("{id}")
    public YxzxResult removeById(@ApiParam(name = "id", value = "课程ID", required = true)
                                 @PathVariable String id){
        boolean result = courseService.removeCourseById(id);
        return result ? YxzxResult.ok() : YxzxResult.error();
    }

    @ApiOperation("根据ID查询课程发布信息")
    @GetMapping("selectCoursePublishVoById/{courseId}")
    public YxzxResult selectCoursePublishVoById(@PathVariable String courseId){
        return YxzxResult.ok().data("item",courseService.selectCoursePublishVoById(courseId));
    }

    @ApiOperation("发布课程")
    @PutMapping("publishCourse/{id}")
    public YxzxResult publishCourse(@ApiParam(name = "id", value = "课程ID", required = true)
                                        @PathVariable String id){
        EbsCourse ebsCourse = new EbsCourse();
        ebsCourse.setId(id);
        ebsCourse.setStatus(EbsCourse.COURSE_NORMAL);
        boolean b = courseService.updateById(ebsCourse);
        return b ? YxzxResult.ok() : YxzxResult.error();
    }
}

